﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace HangerSupport
{
	internal class HSLabelManage
	{
		public HSLabelManage(UIApplication uiApp)
		{
			this.m_uiApp = uiApp;
			this.m_doc = this.m_uiApp.ActiveUIDocument.Document;
			this.m_selection = this.m_uiApp.ActiveUIDocument.Selection;
		}

		public bool SetHSLabel()
		{
            bool flag = (int)this.m_doc.ActiveView.ViewType != 1;
			bool result;
			if (flag)
			{
				Utils.MessageBox("请在平面视图下进行操作");
				result = false;
			}
			else
			{
				PipeSelect pipeSelect = new PipeSelect(this.m_uiApp);
				Label_Define label_Define = new Label_Define();
				Element element = pipeSelect.SelectSingleHS("请选择支吊架以确定支吊架类型");
				FamilyInstance familyInstance = element as FamilyInstance;
				bool flag2 = familyInstance == null;
				if (flag2)
				{
					result = false;
				}
				else
				{
					string name = familyInstance.Symbol.Family.Name;
					this.GetSetLabel(element, ref label_Define, false);
					bool flag3 = label_Define.m_nPosType == 0;
					if (flag3)
					{
						label_Define = HSLabelManage.m_LabelDef;
					}
					DlgHSLabelSet dlgHSLabelSet = new DlgHSLabelSet(label_Define);
					bool flag4 = DialogResult.OK != dlgHSLabelSet.ShowDialog();
					if (flag4)
					{
						result = false;
					}
					else
					{
						label_Define = dlgHSLabelSet.m_labelDef;
						dlgHSLabelSet.Close();
						HSLabelManage.m_LabelDef = label_Define;
						switch (dlgHSLabelSet.m_labelDrawType)
						{
						case DlgHSLabelSet.Label_HS_Draw_Type.eDrawTypeContinueSingleSelect:
						{
							long num = label_Define.GetNO();
							for (;;)
							{
								Element element2 = pipeSelect.SelectSingleHSFamily(name, "请选择支吊架");
								bool flag5 = element2 == null;
								if (flag5)
								{
									break;
								}
								num = this.GetRightLabelNO(name, num, element2);
								label_Define.SetNO(num);
								Transaction transaction = new Transaction(this.m_doc, "Label");
								transaction.Start();
								this.GetSetLabel(element2, ref label_Define, true);
								transaction.Commit();
								num += 1L;
							}
							break;
						}
						case DlgHSLabelSet.Label_HS_Draw_Type.eDrawTypeMultiSelectAuto:
						{
							long num2 = label_Define.GetNO();
							List<Element> list = pipeSelect.SelectMultiHSFamily(name, "请选择支吊架");
							for (int i = 0; i < list.Count; i++)
							{
								Element ele = list[i];
								num2 = this.GetRightLabelNO(name, num2, list);
								label_Define.SetNO(num2);
								Transaction transaction2 = new Transaction(this.m_doc, "Label");
								transaction2.Start();
								this.GetSetLabel(ele, ref label_Define, true);
								transaction2.Commit();
								list.RemoveAt(0);
								i--;
								num2 += 1L;
							}
							break;
						}
						case DlgHSLabelSet.Label_HS_Draw_Type.eDrawTypeSetSameLabel:
						{
							Transaction transaction3 = new Transaction(this.m_doc, "Label");
							transaction3.Start();
							List<Element> list2 = pipeSelect.SelectMultiHSFamily(name, "请选择支吊架");
							foreach (Element ele2 in list2)
							{
								this.GetSetLabel(ele2, ref label_Define, true);
							}
							transaction3.Commit();
							break;
						}
						case DlgHSLabelSet.Label_HS_Draw_Type.eDrawTypeChangeCurSel:
						{
							Transaction transaction4 = new Transaction(this.m_doc, "Label");
							transaction4.Start();
							this.GetSetLabel(element, ref label_Define, true);
							transaction4.Commit();
							break;
						}
						}
						result = true;
					}
				}
			}
			return result;
		}

		public void GetSetLabel(Element ele, ref Label_Define def, bool bSet = true)
		{
			try
			{
				string labelTol = def.GetLabelTol();
				HSDraw.SetGetParValue(ele, "编号", ref def.m_strNo, bSet);
				HSDraw.SetGetParValue(ele, "前缀", ref def.m_strPre, bSet);
				HSDraw.SetGetParValue(ele, "整体编号", ref labelTol, bSet);
				HSDraw.SetGetParValue(ele, "显示位置", ref def.m_nPosType, bSet);
				HSDraw.SetGetParValue(ele, "编号类型", ref def.m_strTypeView, bSet);
			}
			catch (Exception ex)
			{
			}
		}

		public int GetMaxLabelNO(string strFamilyName)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			FilteredElementCollector filteredElementCollector2 = filteredElementCollector.WhereElementIsNotElementType();
			IList<Element> list = filteredElementCollector2.ToElements();
			int num = 1;
			foreach (Element element in list)
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				bool flag = familyInstance == null;
				if (!flag)
				{
					FamilySymbol symbol = familyInstance.Symbol;
					bool flag2 = symbol == null;
					if (!flag2)
					{
						string name = symbol.Family.Name;
						bool flag3 = name != strFamilyName;
						if (!flag3)
						{
							string s = "1";
							HSDraw.SetGetParValue(element, "编号", ref s, false);
							int num2 = int.Parse(s);
							bool flag4 = num < num2;
							if (flag4)
							{
								num = num2;
							}
						}
					}
				}
			}
			return num;
		}

		public bool IsLabelNOExist(string strFamilyName, long nNO)
		{
			List<Element> lstEleKnown = new List<Element>();
			return this.IsLabelNOExist(strFamilyName, nNO, lstEleKnown);
		}

		public bool IsLabelNOExist(string strFamilyName, long nNO, List<Element> lstEleKnown)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			FilteredElementCollector filteredElementCollector2 = filteredElementCollector.WhereElementIsNotElementType();
			IList<Element> list = filteredElementCollector2.ToElements();
			foreach (Element item in lstEleKnown)
			{
				list.Remove(item);
			}
			foreach (Element element in list)
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				bool flag = familyInstance == null;
				if (!flag)
				{
					FamilySymbol symbol = familyInstance.Symbol;
					bool flag2 = symbol == null;
					if (!flag2)
					{
						string name = symbol.Family.Name;
						bool flag3 = name != strFamilyName;
						if (!flag3)
						{
							bool flag4 = false;
							foreach (Element element2 in lstEleKnown)
							{
								bool flag5 = element.Id == element2.Id;
								if (flag5)
								{
									flag4 = true;
									break;
								}
							}
							bool flag6 = flag4;
							if (!flag6)
							{
								int num = 0;
								HSDraw.SetGetParValue(element, "显示位置", ref num, false);
								bool flag7 = num == 0;
								if (!flag7)
								{
									string s = "1";
									HSDraw.SetGetParValue(element, "编号", ref s, false);
									int num2 = int.Parse(s);
									bool flag8 = nNO == (long)num2;
									if (flag8)
									{
										return true;
									}
								}
							}
						}
					}
				}
			}
			return false;
		}

		public long GetRightLabelNO(string strFamilyName, long nNOStart, List<Element> lstEleKnown)
		{
			long num = nNOStart;
			while (this.IsLabelNOExist(strFamilyName, num, lstEleKnown))
			{
				num += 1L;
			}
			return num;
		}

		public long GetRightLabelNO(string strFamilyName, long nNOStart, Element eleCur)
		{
			List<Element> list = new List<Element>();
			list.Add(eleCur);
			long num = nNOStart;
			while (this.IsLabelNOExist(strFamilyName, num, list))
			{
				num += 1L;
			}
			return num;
		}

		private UIApplication m_uiApp;

		private Document m_doc;

		private Selection m_selection;

		private static Label_Define m_LabelDef = new Label_Define();
	}
}
